// created on 25.07.2007 at 12:22
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Designer;
using Gdk;

public class InstanceEditMenu : Menu
{
	public Inst inst;
} 
public class RoomEditTab : VBox, iEditTab
{
	Gtk.Action apply;
	Room editing;
	int pos;
	VBox vbox7;
	Gtk.ComboBox chooseObjectCombo;
	Gtk.ScrolledWindow scrolledwindow6;
	Gtk.DrawingArea chooseObjectDrawing;
	private Gtk.HBox hbox3;
    private Gtk.Toolbar toolbar1;
    private Gtk.Label label7;
    private NumEntry entryGridX;
    private Gtk.Label label9;
    private Gtk.Label label8;
    private NumEntry entryGridY;
    private Gtk.HPaned hpaned1;
    private Gtk.Notebook notebook1;
    private Gtk.VBox vbox5;
    private Gtk.Table table3;
    private ResNameEntry entryName;
    private NumEntry entryFps;
    private Gtk.Entry entryCaption;
    private NumEntry entryWidth;
    private NumEntry entryHeight;
    private Gtk.Label label15;
    private Gtk.Label label16;
    private Gtk.Label label17;
    private Gtk.Label label18;
    private Gtk.Label label19;
    private Gtk.CheckButton checkPersistent;
    private Gtk.Label label13;
    private Gtk.Label label14;
    private Gtk.ScrolledWindow scrolledwindow5;
    private Gtk.DrawingArea drawingarea1;
    private Gtk.VBox vbox6;
    private Gtk.HBox hbox4;
    private Gtk.Label label21;
    private Gtk.ColorButton colorbutton3;
	public RoomEditTab(Room lev)
	{
		this.editing = lev;
        this.Name = "vbox4";
        // Container child vbox4.Gtk.Box+BoxChild
        this.hbox3 = new Gtk.HBox();
        this.hbox3.Name = "hbox3";
        // Container child hbox3.Gtk.Box+BoxChild
        Gtk.UIManager w1 = new Gtk.UIManager();
		Gtk.ActionGroup w2 = new Gtk.ActionGroup("Default");
		this.apply = new Gtk.Action("apply", null, null, "gtk-apply");
        w2.Add(this.apply, null);
        w1.InsertActionGroup(w2, 0);
        w1.AddUiFromString("<ui><toolbar name='toolbar1'><toolitem action='apply'/></toolbar></ui>");
        this.toolbar1 = ((Gtk.Toolbar)(w1.GetWidget("/toolbar1")));
        this.toolbar1.Name = "toolbar1";
        this.toolbar1.ShowArrow = false;
        this.toolbar1.ToolbarStyle = ((Gtk.ToolbarStyle)(0));
        this.hbox3.Add(this.toolbar1);
        Gtk.Box.BoxChild w54 = ((Gtk.Box.BoxChild)(this.hbox3[this.toolbar1]));
        w54.Position = 0;
        // Container child hbox3.Gtk.Box+BoxChild
        this.label7 = new Gtk.Label();
        this.label7.Name = "label7";
        this.label7.LabelProp = "Gitter (X):";
        this.hbox3.Add(this.label7);
        Gtk.Box.BoxChild w55 = ((Gtk.Box.BoxChild)(this.hbox3[this.label7]));
        w55.Position = 1;
        w55.Expand = false;
        w55.Fill = false;
        // Container child hbox3.Gtk.Box+BoxChild
        //GRID X
        this.entryGridX = new NumEntry();
        this.entryGridX.WidthRequest = 30;
        this.entryGridX.CanFocus = true;
        this.entryGridX.Name = "entryGridX";
        this.entryGridX.IsEditable = true;
        this.entryGridX.InvisibleChar = '•';
        this.entryGridX.Num = (int)editing.Settings["gridx"];
        this.hbox3.Add(this.entryGridX);
        Gtk.Box.BoxChild w56 = ((Gtk.Box.BoxChild)(this.hbox3[this.entryGridX]));
        w56.Position = 2;
        // Container child hbox3.Gtk.Box+BoxChild
        this.label9 = new Gtk.Label();
        this.label9.WidthRequest = 20;
        this.label9.Name = "label9";
        this.label9.LabelProp = "";
        this.hbox3.Add(this.label9);
        Gtk.Box.BoxChild w57 = ((Gtk.Box.BoxChild)(this.hbox3[this.label9]));
        w57.Position = 3;
        w57.Expand = false;
        w57.Fill = false;
        // Container child hbox3.Gtk.Box+BoxChild
        this.label8 = new Gtk.Label();
        this.label8.Name = "label8";
        this.label8.LabelProp = "Gitter (Y):";
        this.hbox3.Add(this.label8);
        Gtk.Box.BoxChild w58 = ((Gtk.Box.BoxChild)(this.hbox3[this.label8]));
        w58.Position = 4;
        w58.Expand = false;
        w58.Fill = false;
        // Container child hbox3.Gtk.Box+BoxChild
        //GRID Y
        this.entryGridY = new NumEntry();
        this.entryGridY.WidthRequest = 30;
        this.entryGridY.CanFocus = true;
        this.entryGridY.Name = "entryGridY";
        this.entryGridY.IsEditable = true;
        this.entryGridY.InvisibleChar = '•';
        this.entryGridY.Num = (int)editing.Settings["gridy"];
        this.hbox3.Add(this.entryGridY);
        Gtk.Box.BoxChild w59 = ((Gtk.Box.BoxChild)(this.hbox3[this.entryGridY]));
        w59.Position = 5;
        this.Add(this.hbox3);
        Gtk.Box.BoxChild w60 = ((Gtk.Box.BoxChild)(this[this.hbox3]));
        w60.Position = 0;
        w60.Expand = false;
        w60.Fill = false;
        // Container child vbox4.Gtk.Box+BoxChild
        this.hpaned1 = new Gtk.HPaned();
        this.hpaned1.CanFocus = true;
        this.hpaned1.Name = "hpaned1";
        this.hpaned1.Position = 146;
        // Container child hpaned1.Gtk.Paned+PanedChild
        this.notebook1 = new Gtk.Notebook();
        this.notebook1.CanFocus = true;
        this.notebook1.Name = "notebook1";
        // Notebook tab
        // Container child notebook1.Gtk.Notebook+NotebookChild
        this.vbox5 = new Gtk.VBox();
        this.vbox5.CanFocus = false;
        this.vbox5.Name = "vbox5";
        // Container child vbox5.Gtk.Box+BoxChild
        this.table3 = new Gtk.Table(((uint)(5)), ((uint)(2)), false);
        this.table3.CanFocus = false;
        this.table3.Name = "table3";
        // Container child table3.Gtk.Table+TableChild
        //NAME
        this.entryName = new ResNameEntry();
        this.entryName.Name = "entryName";
        this.entryName.IsEditable = true;
        this.entryName.InvisibleChar = '•';
        this.entryName.WidthRequest = 75;
        entryName.ValidText = editing.Name;
        this.table3.Add(this.entryName);
        Gtk.Table.TableChild w61 = ((Gtk.Table.TableChild)(this.table3[this.entryName]));
        w61.LeftAttach = ((uint)(1));
        w61.RightAttach = ((uint)(2));
        w61.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        //FPS
        this.entryFps = new NumEntry();
        this.entryFps.Name = "entryFps";
        this.entryFps.IsEditable = true;
        this.entryFps.MaxLength = 3;
        this.entryFps.InvisibleChar = '•';
        this.entryFps.WidthRequest = 75;
        entryFps.MaxLength = 3;
        entryFps.Num = (int) editing.Settings["speed"];
        this.table3.Add(this.entryFps);
        Gtk.Table.TableChild w62 = ((Gtk.Table.TableChild)(this.table3[this.entryFps]));
        w62.TopAttach = ((uint)(4));
        w62.BottomAttach = ((uint)(5));
        w62.LeftAttach = ((uint)(1));
        w62.RightAttach = ((uint)(2));
        w62.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        //CAPTION
        this.entryCaption = new Entry();
        this.entryCaption.Name = "entryCaption";
        this.entryCaption.IsEditable = true;
        this.entryCaption.InvisibleChar = '•';
        this.entryCaption.WidthRequest = 75;
        this.entryCaption.Text = (string) editing.Settings["caption"];
        this.table3.Add(this.entryCaption);
        Gtk.Table.TableChild w63 = ((Gtk.Table.TableChild)(this.table3[this.entryCaption]));
        w63.TopAttach = ((uint)(1));
        w63.BottomAttach = ((uint)(2));
        w63.LeftAttach = ((uint)(1));
        w63.RightAttach = ((uint)(2));
        w63.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        //WIDTH
        this.entryWidth = new NumEntry();
        this.entryWidth.Name = "entryWidth";
        this.entryWidth.IsEditable = true;
        this.entryWidth.InvisibleChar = '•';
        this.entryWidth.WidthRequest = 75;
        entryWidth.Num = (int) editing.Settings["width"];
        oldWidth = entryWidth.Num;
        this.table3.Add(this.entryWidth);
        Gtk.Table.TableChild w64 = ((Gtk.Table.TableChild)(this.table3[this.entryWidth]));
        w64.TopAttach = ((uint)(2));
        w64.BottomAttach = ((uint)(3));
        w64.LeftAttach = ((uint)(1));
        w64.RightAttach = ((uint)(2));
        w64.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        //HEIGHT
        this.entryHeight = new NumEntry();
        this.entryHeight.Name = "entryHeight";
        this.entryHeight.IsEditable = true;
        this.entryHeight.InvisibleChar = '•';
        this.entryHeight.WidthRequest = 75;
        entryHeight.Num = (int) editing.Settings["height"];
        oldHeight = entryHeight.Num;
        this.table3.Add(this.entryHeight);
        Gtk.Table.TableChild w65 = ((Gtk.Table.TableChild)(this.table3[this.entryHeight]));
        w65.TopAttach = ((uint)(3));
        w65.BottomAttach = ((uint)(4));
        w65.LeftAttach = ((uint)(1));
        w65.RightAttach = ((uint)(2));
        w65.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        this.label15 = new Gtk.Label();
        this.label15.CanFocus = false;
        this.label15.Name = "label15";
        this.label15.LabelProp = "Name:";
        this.table3.Add(this.label15);
        Gtk.Table.TableChild w66 = ((Gtk.Table.TableChild)(this.table3[this.label15]));
        w66.XOptions = ((Gtk.AttachOptions)(4));
        w66.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        this.label16 = new Gtk.Label();
        this.label16.CanFocus = false;
        this.label16.Name = "label16";
        this.label16.LabelProp = "Titel:";
        this.table3.Add(this.label16);
        Gtk.Table.TableChild w67 = ((Gtk.Table.TableChild)(this.table3[this.label16]));
        w67.TopAttach = ((uint)(1));
        w67.BottomAttach = ((uint)(2));
        w67.XOptions = ((Gtk.AttachOptions)(4));
        w67.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        this.label17 = new Gtk.Label();
        this.label17.CanFocus = false;
        this.label17.Name = "label17";
        this.label17.LabelProp = "Breite:";
        this.table3.Add(this.label17);
        Gtk.Table.TableChild w68 = ((Gtk.Table.TableChild)(this.table3[this.label17]));
        w68.TopAttach = ((uint)(2));
        w68.BottomAttach = ((uint)(3));
        w68.XOptions = ((Gtk.AttachOptions)(4));
        w68.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        this.label18 = new Gtk.Label();
        this.label18.CanFocus = false;
        this.label18.Name = "label18";
        this.label18.LabelProp = "Höhe:";
        this.table3.Add(this.label18);
        Gtk.Table.TableChild w69 = ((Gtk.Table.TableChild)(this.table3[this.label18]));
        w69.TopAttach = ((uint)(3));
        w69.BottomAttach = ((uint)(4));
        w69.XOptions = ((Gtk.AttachOptions)(4));
        w69.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table3.Gtk.Table+TableChild
        this.label19 = new Gtk.Label();
        this.label19.CanFocus = false;
        this.label19.Name = "label19";
        this.label19.LabelProp = "FPS:";
        this.table3.Add(this.label19);
        Gtk.Table.TableChild w70 = ((Gtk.Table.TableChild)(this.table3[this.label19]));
        w70.TopAttach = ((uint)(4));
        w70.BottomAttach = ((uint)(5));
        w70.XOptions = ((Gtk.AttachOptions)(4));
        w70.YOptions = ((Gtk.AttachOptions)(4));
        this.vbox5.Add(this.table3);
        Gtk.Box.BoxChild w71 = ((Gtk.Box.BoxChild)(this.vbox5[this.table3]));
        w71.Position = 0;
        w71.Expand = false;
        w71.Fill = false;
        // Container child vbox5.Gtk.Box+BoxChild
        this.checkPersistent = new Gtk.CheckButton();
        this.checkPersistent.Name = "checkPersistent";
        this.checkPersistent.Label = "Bleibend";
        this.checkPersistent.DrawIndicator = true;
        this.checkPersistent.UseUnderline = true;
        this.checkPersistent.Active = (bool)editing.Settings["persistent"];
        this.vbox5.Add(this.checkPersistent);
        Gtk.Box.BoxChild w72 = ((Gtk.Box.BoxChild)(this.vbox5[this.checkPersistent]));
        w72.Position = 1;
        w72.Expand = false;
        w72.Fill = false;
        // Notebook tab
        this.label13 = new Gtk.Label();
        this.label13.CanFocus = false;
        this.label13.Name = "label13";
        this.label13.LabelProp = "Allgemein";
        this.notebook1.AppendPage(vbox5, label13);
        Gtk.Notebook.NotebookChild w73 = ((Gtk.Notebook.NotebookChild)(this.notebook1[this.vbox5]));
        w73.TabExpand = false;
        // Notebook tab
        
        this.vbox7 = new Gtk.VBox();
        this.vbox7.Name = "vbox7";
        // Container child vbox7.Gtk.Box+BoxChild
        this.chooseObjectCombo = Gtk.ComboBox.NewText();
        this.chooseObjectCombo.Name = "chooseObjectCombo";
        this.vbox7.Add(this.chooseObjectCombo);
        Label label22 = new Gtk.Label();
        label22.Name = "label22";
        label22.LabelProp = "<span size=\"small\"><i>Linksklick: hinzufügen\n\nRechtsklick: Kontextmenü\n + Strg: Alle Instanzen\n\tan Position löschen</i></span>";
        label22.UseMarkup = true;
        label22.Wrap = true;
        vbox7.Add(label22);
        Gtk.Box.BoxChild w020 = ((Gtk.Box.BoxChild)(this.vbox7[this.chooseObjectCombo]));
        w020.Position = 0;
        w020.Expand = false;
        w020.Fill = false;
        // Container child vbox7.Gtk.Box+BoxChild
        this.scrolledwindow6 = new Gtk.ScrolledWindow();
        this.scrolledwindow6.CanFocus = true;
        this.scrolledwindow6.Name = "scrolledwindow6";
        this.scrolledwindow6.VscrollbarPolicy = ((Gtk.PolicyType)(1));
        this.scrolledwindow6.HscrollbarPolicy = ((Gtk.PolicyType)(1));
        // Container child scrolledwindow6.Gtk.Container+ContainerChild
        Gtk.Viewport w021 = new Gtk.Viewport();
        w021.Name = "GtkViewport1";
        w021.ShadowType = ((Gtk.ShadowType)(0));
        // Container child GtkViewport1.Gtk.Container+ContainerChild
        this.chooseObjectDrawing = new Gtk.DrawingArea();
        this.chooseObjectDrawing.Name = "chooseObjectDrawing";
        w021.Add(this.chooseObjectDrawing);
        this.scrolledwindow6.Add(w021);
        this.vbox7.Add(this.scrolledwindow6);
        Gtk.Box.BoxChild w022 = ((Gtk.Box.BoxChild)(this.vbox7[this.scrolledwindow6]));
        w022.Position = 1;
        this.label14 = new Gtk.Label();
        this.label14.CanFocus = false;
        this.label14.Name = "label14";
        this.label14.LabelProp = "Objekte";
        this.notebook1.AppendPage(vbox7, label14);
        
        this.vbox6 = new Gtk.VBox();
		this.vbox6.CanFocus = false;
		this.vbox6.Name = "vbox6";
		// Container child vbox6.Gtk.Box+BoxChild
		this.hbox4 = new Gtk.HBox();
		this.hbox4.CanFocus = false;
		this.hbox4.Name = "hbox4";
		// Container child hbox4.Gtk.Box+BoxChild
		this.label21 = new Gtk.Label();
		this.label21.CanFocus = false;
		this.label21.Name = "label21";
		this.label21.LabelProp = "Farbe:";
		this.hbox4.Add(this.label21);
		Gtk.Box.BoxChild w75 = ((Gtk.Box.BoxChild)(this.hbox4[this.label21]));
		w75.Position = 0;
		w75.Expand = false;
		w75.Fill = false;
		// Container child hbox4.Gtk.Box+BoxChild
		this.colorbutton3 = new Gtk.ColorButton();
		this.colorbutton3.Events = ((Gdk.EventMask)(784));
		this.colorbutton3.Name = "colorbutton3";
		this.hbox4.Add(this.colorbutton3);
		Gtk.Box.BoxChild w76 = ((Gtk.Box.BoxChild)(this.hbox4[this.colorbutton3]));
		w76.Position = 1;
		w76.Expand = false;
		w76.Fill = false;
		this.vbox6.Add(this.hbox4);
		Gtk.Box.BoxChild w77 = ((Gtk.Box.BoxChild)(this.vbox6[this.hbox4]));
		w77.Position = 0;
		w77.Expand = false;
		w77.Fill = false;
		// Notebook tab
		Label label20 = new Gtk.Label();
		label20.CanFocus = false;
		label20.Name = "label20";
		label20.LabelProp = "Hintergrund";
		this.notebook1.AppendPage(this.vbox6, label20);
		Gtk.Notebook.NotebookChild w78 = ((Gtk.Notebook.NotebookChild)(this.notebook1[this.vbox6]));
		w78.TabExpand = false;
        
        this.notebook1.ShowAll();
        this.hpaned1.Add(this.notebook1);
        this.notebook1.CurrentPage = 0;
        this.notebook1.Scrollable = true;
        Gtk.Paned.PanedChild w010 = ((Gtk.Paned.PanedChild)(this.hpaned1[this.notebook1]));
        w010.Resize = false;
        // Container child hpaned1.Gtk.Paned+PanedChild
        this.scrolledwindow5 = new Gtk.ScrolledWindow();
        this.scrolledwindow5.CanFocus = true;
        this.scrolledwindow5.Name = "scrolledwindow5";
        this.scrolledwindow5.VscrollbarPolicy = ((Gtk.PolicyType)(1));
        this.scrolledwindow5.HscrollbarPolicy = ((Gtk.PolicyType)(1));
        // Container child scrolledwindow5.Gtk.Container+ContainerChild
        Gtk.Viewport w001 = new Gtk.Viewport();
        w001.Name = "GtkViewport";
        w001.ShadowType = ((Gtk.ShadowType)(0));
        // Container child GtkViewport.Gtk.Container+ContainerChild
        this.drawingarea1 = new Gtk.DrawingArea();
        this.drawingarea1.Name = "drawingarea1";
        w001.Add(this.drawingarea1);
        this.scrolledwindow5.Add(w001);
        this.hpaned1.Add(this.scrolledwindow5);
        this.Add(this.hpaned1);
        Gtk.Box.BoxChild w002 = ((Gtk.Box.BoxChild)(this[this.hpaned1]));
        w002.Position = 1;
        
        CloseableTab ctab = new CloseableTab("Ebene: "+editing.Name);
        ctab.closeButton.Clicked += OnCloseActivated;
        pos = ResourceMgr.Instance.Win.editTabs.AppendPage(this, ctab);
		Gtk.Notebook.NotebookChild w003 = ((Gtk.Notebook.NotebookChild)(ResourceMgr.Instance.Win.editTabs[this]));
		w003.TabExpand = false;
		ResourceMgr.Instance.Win.editTabs.ShowAll();
		
		this.drawingarea1.ExposeEvent += OnDrawingExposeEvent;
		this.drawingarea1.Events = Gdk.EventMask.AllEventsMask;
		this.drawingarea1.ButtonPressEvent += OnDrawingButtonPress;
		this.drawingarea1.MotionNotifyEvent += OnDrawingMotion;
		this.apply.Activated += OnApplyActivated;
		this.entryWidth.FocusOutEvent += OnWidthEntryFocusOut;
		this.entryHeight.FocusOutEvent += OnHeightEntryFocusOut;
		this.entryGridX.FocusOutEvent += OnGridEntryFocusOut;
		this.entryGridY.FocusOutEvent += OnGridEntryFocusOut;
		this.colorbutton3.ColorSet += OnColorbuttonColorSet;
		this.KeyPressEvent += OnKeyPress;
		this.KeyReleaseEvent += OnKeyRelease;
		
		this.entryFps.MinValue = 1;
		this.entryGridX.MinValue = 0;
		this.entryGridY.MinValue = 0;
		this.drawingarea1.HeightRequest = oldHeight;
		this.drawingarea1.WidthRequest = oldWidth;
		
		bgColor = (Gdk.Color) editing.Settings["bgcolor"];
		this.colorbutton3.Color = bgColor;
		//copy instance list
		foreach(Inst inst in editing.instances)
		{
			InstanceList.Add(new Inst(inst));
		}
		
		Reload();
	}
	bool modifierPressed = false;
	protected void OnKeyPress(object o, Gtk.KeyPressEventArgs args)
	{
		Gdk.Key key = args.Event.Key;
		if(key == Gdk.Key.Control_L || key == Gdk.Key.Control_R)
			modifierPressed = true;
	}
	
	protected void OnKeyRelease(object o, Gtk.KeyReleaseEventArgs args)
	{
		Gdk.Key key = args.Event.Key;
		if(key == Gdk.Key.Control_L || key == Gdk.Key.Control_R)
			modifierPressed = false;
	}
	
	List<Obj> objectList = new List<Obj>();
	Gdk.Color bgColor;
	protected void OnColorbuttonColorSet(object o, EventArgs args)
	{
		bgColor = this.colorbutton3.Color;
		DrawRoom();
	}
	protected void OnApplyActivated(object o, EventArgs args)
	{
		ResourceMgr.Instance.Win.CloseEditTab(editing, true);
	}
	protected void OnCloseActivated(object o, EventArgs args)
	{
		Gtk.MessageDialog message = new MessageDialog(ResourceMgr.Instance.Win, DialogFlags.Modal, MessageType.Question, ButtonsType.None, false, "");
		message.Text = "Sollen die Änderungen übernommen werden?";
		message.Title = "Änderungen übernehmen?";
		message.AddButton(Stock.Yes, ResponseType.Yes);
		message.AddButton(Stock.No, ResponseType.No);
		message.AddButton(Stock.Cancel, ResponseType.Cancel);
		ResponseType RetVal = (ResponseType)message.Run();
		message.Destroy();
		if(RetVal == ResponseType.Yes || RetVal == ResponseType.No)
		{
			ResourceMgr.Instance.Win.CloseEditTab(editing, (RetVal == ResponseType.Yes));
		}
	}
	public int GetPosition()
	{
		return pos;
	}
	public void SetPosition(int pos)
	{
		this.pos = pos;
	}
	List<Inst> InstanceList = new List<Designer.Inst>();
	public void Close(bool save)
	{
		if(save)
		{
		editing.Settings["speed"] = entryFps.Num;
		editing.Settings["caption"] = entryCaption.Text;
		editing.Settings["width"] = entryWidth.Num;
		editing.Settings["height"] = entryHeight.Num;
		editing.Settings["persistent"] = checkPersistent.Active;
		editing.Settings["gridx"] = entryGridX.Num;
		editing.Settings["gridy"] = entryGridY.Num;
		editing.Settings["bgcolor"] = bgColor;
		if(objectList.Count > 0)
			editing.Settings["lastobject"] = (Obj)objectList[chooseObjectCombo.Active];
		editing.instances = InstanceList;
		ResourceMgr.Instance.ChangeResourceName(editing, entryName.ValidText);
		}
		
		ResourceMgr.Instance.Win.editTabs.Remove(this);
	}
	void GetGridCoords(int x, int y, out int gx, out int gy)
	{
		if(entryGridX.Num != 0)
			gx = (int)(x / this.entryGridX.Num)*entryGridX.Num;
		else
			gx = (int)x;
			
		if(entryGridY.Num != 0)
			gy = (int)(y / this.entryGridY.Num)*entryGridY.Num;
		else
			gy = (int)y;
	}
	void GetHoverList(int x, int y)
	{
		HoverList.Clear();
		foreach(Inst inst in InstanceList)
		{
			if(inst.CheckHover(x,y))
			HoverList.Add(inst);
		}
	}
	List<Inst> HoverList = new List<Inst>();
	protected void OnDrawingMotion(object o, Gtk.MotionNotifyEventArgs args)
	{
		Gdk.EventMotion motion = args.Event;
		int gridX, gridY;
		int X = (int) motion.X;
		int Y = (int) motion.Y;
		GetGridCoords(X, Y, out gridX, out gridY);
//		GetHoverList(X,Y);
//		if(HoverList.Count > 0)
//		{
//			Console.WriteLine("Mouse currently at "+gridX+"/"+gridY+",  instances: ");
//			foreach(Inst inst in HoverList)
//			{
//				Console.Write(inst.obj.name+", ");
//			}
//			Console.WriteLine();
//		}
	}
	protected void OnDrawingButtonPress(object o, Gtk.ButtonPressEventArgs args)
	{
		Gdk.EventButton eventbutton = args.Event;
		
		if((int)eventbutton.X >= oldWidth || (int)eventbutton.Y >= oldHeight) return;
		
		if(eventbutton.Button == 1 && notebook1.CurrentPage == 1 && objectList.Count != 0)
		{
			Obj curObj = objectList[chooseObjectCombo.Active];
			int gridX, gridY;
			GetGridCoords((int)eventbutton.X, (int)eventbutton.Y, out gridX, out gridY);
				
			Console.WriteLine("Adding instance of "+curObj.Name+" at "+gridX+"/"+gridY+" to room "+editing.Name);
			InstanceList.Add(new Inst(curObj, gridX, gridY));
			DrawRoom();
		}
		else if(eventbutton.Button == 3 && notebook1.CurrentPage == 1)
		{
			GetHoverList((int)eventbutton.X, (int)eventbutton.Y);
			if(!modifierPressed && HoverList.Count > 0)
			{
				Menu menu = new Menu();
				menu.Events = Gdk.EventMask.AllEventsMask;
				menu.KeyPressEvent += OnKeyPress;
				menu.KeyReleaseEvent += OnKeyRelease;
				MenuItem mitem;
				foreach(Inst inst in HoverList)
				{
					mitem = new MenuItem("Instanz von "+inst.obj.Name);
					InstanceEditMenu submenu = new InstanceEditMenu();
					submenu.Events = Gdk.EventMask.AllEventsMask;
					submenu.KeyPressEvent += OnKeyPress;
					submenu.KeyReleaseEvent += OnKeyRelease;
					submenu.inst = inst;
					MenuItem submitem;
					submitem = new MenuItem("Entfernen");
					submitem.Activated += OnInstanceRemoveActivated;
					submenu.Append(submitem);
					submitem = new MenuItem("Objekt bearbeiten");
					submitem.Activated += OnInstanceEditActivated;
					submenu.Append(submitem);
					mitem.Submenu = submenu;
					menu.Append(mitem);
				}
				menu.ShowAll();
				menu.Popup();
			}
			else if(HoverList.Count > 0 )
			{
				foreach(Inst inst in HoverList)
				{
					InstanceList.Remove(inst);
				}
			}
			DrawRoom();
		}
	}
	protected void OnInstanceRemoveActivated(object o, EventArgs args)
	{
		MenuItem sender = (MenuItem) o;
		InstanceEditMenu imenu = (InstanceEditMenu)sender.Parent;
		InstanceList.Remove(imenu.inst);
		DrawRoom();
	}
	protected void OnInstanceEditActivated(object o, EventArgs args)
	{
		MenuItem sender = (MenuItem) o;
		InstanceEditMenu imenu = (InstanceEditMenu)sender.Parent;
		ResourceMgr.Instance.Win.ShowEditTab(imenu.inst.obj);
		DrawRoom();
	}
	
	void DrawRoom()
	{
		Gdk.Drawable drawable  = this.drawingarea1.GdkWindow;
		using(Gdk.GC gc = new Gdk.GC(drawable))
		{
      		Gdk.Colormap colormap = Gdk.Colormap.System;
      		
      		Gdk.Color black_color = new Gdk.Color(0, 0, 0);
      		colormap.AllocColor (ref black_color, true, true);
      		Gdk.Color white_color = new Gdk.Color(255, 255, 255);
      		colormap.AllocColor (ref white_color, true, true);
      		Gdk.Color red_color = new Gdk.Color(255, 0, 0);
      		colormap.AllocColor (ref red_color, true, true);
      		Gdk.Color back_color = bgColor;
      		colormap.AllocColor(ref back_color, true, true);
      		
      		gc.Foreground = back_color;
//      	Pango.Layout layout = new Pango.Layout(PangoContext);
//      	layout.FontDescription = Pango.FontDescription.FromString("Arial 12");
//      	layout.SetText("This is my string!");
			drawable.DrawRectangle(gc, true, 0,0,oldWidth, oldHeight);
			//Draw Instances
			gc.Foreground = red_color;
			foreach(Inst inst in InstanceList)
			{
				if(inst.x >= 0 && inst.x < oldWidth && inst.y >= 0 && inst.y < oldHeight)
				drawable.DrawRectangle(gc, true, inst.x, inst.y, 10, 10);
			}
			
			//Draw Grid
			gc.Foreground = white_color;
			int gridx = this.entryGridX.Num;
			int gridy = this.entryGridY.Num;
			if(gridx > 0)
			{
			for(int ix = gridx; ix <= oldWidth; ix += gridx)
				drawable.DrawLine(gc, ix,0,ix, oldHeight);
			}
			if(gridy > 0)
			{
			for(int iy = gridy; iy <= oldHeight; iy += gridy)
				drawable.DrawLine(gc, 0,iy,oldWidth, iy);
			}
		}
	}
	protected virtual void OnDrawingExposeEvent(object o, Gtk.ExposeEventArgs args)
	{
		DrawRoom();
	}
	int oldWidth;
	int oldHeight;
	protected virtual void OnGridEntryFocusOut(object o, EventArgs args)
	{
		DrawRoom();
	}
	protected virtual void OnWidthEntryFocusOut(object o, EventArgs args)
	{
		if(entryWidth.Num < 50)
		{
			entryWidth.Num = oldWidth;
			return;
		}
		if(entryWidth.Num == oldWidth) return;
		oldWidth = entryWidth.Num;
		this.drawingarea1.WidthRequest = oldWidth;
		Console.WriteLine("Changed room width to "+entryWidth.Num.ToString());
		RoomSizeChanged();
	}
	protected virtual void OnHeightEntryFocusOut(object o, EventArgs args)
	{
		if(entryHeight.Num < 50)
		{
			entryHeight.Num = oldHeight;
			return;
		}
		if(entryHeight.Num == oldHeight) return;
		oldHeight = entryHeight.Num;
		this.drawingarea1.HeightRequest = oldHeight;
		Console.WriteLine("Changed room height to "+entryHeight.Num.ToString());
		RoomSizeChanged();
	}
	
	void RoomSizeChanged()
	{
		//Suche Instanzen, die jetzt außerhalb des Raumes liegen und speichere sie in outsideList
		List<Inst> outsideList = new List<Inst>();
		foreach(Inst inst in InstanceList)
		{
			if(!(inst.x >= 0 && inst.x < oldWidth && inst.y >= 0 && inst.y < oldHeight))
			outsideList.Add(inst);
		}
		//Wenn Instanzen außerhalb des Raumes liegen, frage nach ob sie entfernt werden sollen
		if(outsideList.Count > 0)
		{
			MessageDialog message = new MessageDialog(ResourceMgr.Instance.Win, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, false, "");
			message.Text = "Nach der Größenänderung befinden sich Instanzen außerhalb des sichtbaren Bereichs. Sollen sie entfernt werden?";
			message.Title = "Instanzen außerhalb der Ebene entfernen?";
			ResponseType RetVal = (ResponseType)message.Run();
			message.Destroy();
			//Wenn "Ja", entferne Instanzen, die in outsideList stehen
			if(RetVal == ResponseType.Yes)
			{
				foreach(Inst inst in outsideList)
				{
					InstanceList.Remove(inst);
				}
			}
		}
	}
	public void DeleteInstanceOfObj(Obj o)
	{
		for(int i = 0; i < InstanceList.Count; i++)
		{
			if(InstanceList[i].obj == o)
			InstanceList.RemoveAt(i--);
		}
		DrawRoom();
	}
	public void Reload()
	{
		entryName.ValidText = editing.Name;
		objectList.Clear();
		this.vbox7.Remove(this.chooseObjectCombo);
		this.chooseObjectCombo = Gtk.ComboBox.NewText();
        this.chooseObjectCombo.Name = "chooseObjectCombo";
        this.vbox7.Add(this.chooseObjectCombo);
        Gtk.Box.BoxChild w020 = ((Gtk.Box.BoxChild)(this.vbox7[this.chooseObjectCombo]));
        w020.Position = 0;
        w020.Expand = false;
        w020.Fill = false;
        if(!ResourceMgr.Instance.ResourceExists((Resource)editing.Settings["lastobject"]))
        	editing.Settings["lastobject"] = null;
        
        int index = 0;
		foreach(Resource Res in ResourceMgr.Instance.ResourceList)
		{
			if(Res.GetResType() == "Object")
			{
				objectList.Add((Obj)Res);
				chooseObjectCombo.AppendText(Res.Name);
				if(editing.Settings["lastobject"] == (Obj)Res)
				{
					index = objectList.Count -1;
				}
			}
		}
		if(objectList.Count != 0)
			chooseObjectCombo.Active = 0;
		if(index > 0)
			chooseObjectCombo.Active = index;
				
		chooseObjectCombo.ShowAll();
	}
}